﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.款式尺寸;
using DXApplicationTangche.UC.款式管理.service;
using DXApplicationTangche.UC.订单一览新.改版;
using DXApplicationTangche.UC.门店下单.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.款式管理
{
    public partial class Frm款式维护预览 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public string styleId { get; set; }
        public string styleNameCn { get; set; }
        private String styleCategoryId { get; set; }
        private String styleCategoryCd { get; set; }
        private String styleCategoryName { get; set; }
        private StyleInfoDto style = new StyleInfoDto();
        private List<StyleCategoryDTO> styleCategoryDTOs;
        private List<ItemDictDTO> itemDictDTOs = new List<ItemDictDTO>();

        public Frm款式维护预览()
        {
            InitializeComponent();
            this.load();
        }

        public Frm款式维护预览(String styleid)
        {
            InitializeComponent();
            this.styleId = styleid;
            this.textBoxstyleid.Text = styleid;
            try { this.style = 款式Service.getStyleInfoByStyleId(Convert.ToInt32(styleid)); } catch { }
            this.load();
            this.显示信息();
        }
        private void load()
        {
            this.styleCategoryDTOs = 款式Service.get款式类别();
            this.searchLookUpEdit款式类别.Properties.DataSource = this.styleCategoryDTOs;
            try
            {
                //this.comboBoxSYTLE_SEASON.DataSource = StyleService.getSYTLE_SEASON();
                this.itemDictDTOs = 款式Service.getItemDictByItemCd("SEASON");
                this.comboBoxSYTLE_SEASON.DataSource = this.itemDictDTOs;
                this.comboBoxSYTLE_SEASON.DisplayMember = "item_name_cn";
            }
            catch { }
        }
        
        private void 显示信息()
        {
            this.textBox款号.Text = this.style.styleNameCn.Split()[0];
            try { this.textBoxSTYLE_NAME_CN.Text = this.style.styleNameCn.Split()[1]; } catch { this.textBoxSTYLE_NAME_CN.Text = this.style.styleNameCn; }
            this.textBoxSYTLE_YEAR.Text = this.style.sytleYear;
            try
            {
                //  季节
                foreach (ItemDictDTO item in this.itemDictDTOs)
                {
                    if ((item.itemCd + "-" + item.itemValue).Equals(this.style.sytleSeason))
                    {
                        this.comboBoxSYTLE_SEASON.SelectedItem = item;
                        this.comboBoxSYTLE_SEASON.Text = item.item_name_cn;
                        break;
                    }
                }
            }
            catch { }
            try { this.pictureBox款式图片.LoadAsync(款式Service.get款式图(this.styleId)); } catch { }
            try
            {
                //  款式类别
                foreach (StyleCategoryDTO item in this.styleCategoryDTOs)
                {
                    if (this.style.styleCategoryCd.Equals(item.code))
                    {
                        this.styleCategoryId = item.id;
                        this.styleCategoryCd = item.code;
                        this.searchLookUpEdit款式类别.Text = "";
                        this.styleCategoryName = item.name;
                        this.searchLookUpEdit款式类别.Text = this.styleCategoryName;
                        break;
                    }
                }
            }
            catch { }
        }

        #region 选择款式类别
        /// <summary>
        /// 绑定清空事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit款式类别_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn款式类别_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        /// <summary>
        /// 点击选择后的行为
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit款式类别View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit款式类别.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.styleCategoryId //  no
                    = this.searchLookUpEdit款式类别.Properties.View.GetRowCellValue(rowHandle, "id").ToString();//id 是 Value Member
                this.styleCategoryCd //  no
                    = this.searchLookUpEdit款式类别.Properties.View.GetRowCellValue(rowHandle, "code").ToString();//id 是 Value Member
                this.styleCategoryName //  no
                    = this.searchLookUpEdit款式类别.Properties.View.GetRowCellValue(rowHandle, "name").ToString();//id 是 Value Member
            }

        }
        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn款式类别_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit款式类别.ToolTip = null;
            searchLookUpEdit款式类别.EditValue = null;
        }
        /// <summary>
        /// 选完之后的显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void searchLookUpEdit款式类别_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.styleCategoryName;
        }
        #endregion

        private void barButtonItem基本信息_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.textBox款号.Text)
                || String.IsNullOrWhiteSpace(this.textBoxSTYLE_NAME_CN.Text)
                || String.IsNullOrWhiteSpace(this.textBoxSYTLE_YEAR.Text)
                || String.IsNullOrWhiteSpace(this.comboBoxSYTLE_SEASON.Text)
                || String.IsNullOrWhiteSpace(this.styleCategoryId)
                || String.IsNullOrWhiteSpace(this.styleCategoryCd)
                )
            {
                MessageBox.Show("款式信息不完整");
                return;
            }
            if (this.textBoxSTYLE_NAME_CN.Text.Contains("）"))
            {
                MessageBox.Show("款式名称不能包含特殊字符 ）");
                return;
            }
            if (this.textBoxSTYLE_NAME_CN.Text.Contains("（"))
            {
                MessageBox.Show("款式名称不能包含特殊字符 （");
                return;
            }
            ItemDictDTO itemDictDTO  = new ItemDictDTO();
            try
            {
                itemDictDTO = this.comboBoxSYTLE_SEASON.SelectedItem as ItemDictDTO;
            }
            catch {
                itemDictDTO.itemCd = "SEASON";
                itemDictDTO.itemValue = "SEASON_10";
            }
            Form改版完成 form = new Form改版完成(
                false,
                new StyleInformationDTO(
                    this.textBox款号.Text.Trim().Replace(" ", ""),
                    this.textBoxSTYLE_NAME_CN.Text.Trim(),
                    this.textBoxSYTLE_YEAR.Text.Trim(),
                    itemDictDTO.itemCd + "-" + itemDictDTO.itemValue,
                    //this.comboBoxSYTLE_SEASON.SelectedValue.ToString(),
                    this.styleCategoryId,
                    this.styleCategoryCd
                    )
                );
            form.ShowDialog();
            if ("200".Equals(form.DTO返回值判断.errorCode))
            {
                StyleInfoDto styleInfoDto = FunctionHelper.JsonDeserialization<StyleInfoDto>(form.DTO返回值判断.errorMessage);
                this.styleId = styleInfoDto.sysStyleId.ToString();
                this.styleNameCn = styleInfoDto.styleNameCn;
                this.style.sysStyleId = styleInfoDto.sysStyleId;
                this.textBoxstyleid.Text = this.styleId;
                MessageBox.Show("新增成功");
            }
            else
            {
                MessageBox.Show("新增失败");
            }
        }

        private void barButtonItem设计点_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new Frm款式维护设计点导入(this.style.styleNameCn).ShowDialog();
        }

        private void barButtonItem尺寸_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            new Frm尺寸(this.style.styleNameCn.Split()[0]).ShowDialog();
        }

        private void pictureBox款式图片_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
            ofd.Title = "请选择图片文件";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                String str = 款式Service.上传款式图(this.styleId, ofd);
                this.pictureBox款式图片.LoadAsync(款式Service.get款式图(this.styleId));
            }
        }

        private void simpleButton顺时针90_Click(object sender, EventArgs e)
        {
            Image img = this.pictureBox款式图片.Image;
            img.RotateFlip(RotateFlipType.Rotate90FlipNone);

            String fileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分") + this.styleId + ".jpg";
            img.Save(@"pic\" + fileName);
            FileService.UploadRequest(
                URL.接口url + "/image/uploadStyleImage?styleId=" + this.styleId,
                @"pic\" + fileName,
                fileName,
                "multipartFile"
                );
            this.pictureBox款式图片.LoadAsync(款式Service.get款式图(this.styleId));
        }

        private void simpleButton逆时针90_Click(object sender, EventArgs e)
        {
            Image img = this.pictureBox款式图片.Image;
            img.RotateFlip(RotateFlipType.Rotate270FlipNone);

            String fileName = DateTime.Now.ToString("yyyy年MM月dd日HH时mm分") + this.styleId + ".jpg";
            img.Save(@"pic\" + fileName);
            FileService.UploadRequest(
                URL.接口url + "/image/uploadStyleImage?styleId=" + this.styleId,
                @"pic\" + fileName,
                fileName,
                "multipartFile"
                );
            this.pictureBox款式图片.LoadAsync(款式Service.get款式图(this.styleId));
        }

    }
}
